# -*- coding: utf-8 -*-

import time
from report import report_sxw
import pooler
from osv import osv
from tools.translate import _
import random
from datetime import datetime
        
class Parser(report_sxw.rml_parse):
        
    def __init__(self, cr, uid, name, context):
        super(Parser, self).__init__(cr, uid, name, context=context)
        pool = pooler.get_pool(cr.dbname)
        user_obj = pool.get('res.users')
        year_obj = pool.get('aas.academic.year')
        self.semester_obj = pool.get('aas.academic.semester')
        
        student_id = context.get('student_id', False)
        self.student = user_obj.browse(cr, uid, student_id, context=context)
        
        self.context = context
        self.study_results = context.get('study_results', {})
        
        all_year_names = self.study_results['details'].keys()
        all_year_ids = year_obj.search(cr, uid, [('name', 'in', all_year_names)])
        all_years = []
        for year in year_obj.browse(cr, uid, all_year_ids, context=context):
            all_years.append([year.name, year.id])
        
        self.stt = 0
        
        self.show_bottom_frame = True
        self.localcontext.update({
            'init': self._init,
            'study_results': self.study_results,
            'show_bottom_frame': self.show_bottom_frame,
            'student': self.student,
            'stt': self._get_stt,
            'reset_stt': self.reset_stt,
            'all_years': all_years,
            'all_semesters': self.get_all_semesters,
            'get_logo': self.get_logo,
        })
    
    def _init(self, student):
#        print student.name
        return True
    
    def get_all_semesters(self, semester_names, year_id):
        semester_names = list(set(semester_names))
        semesters = []
        all_semester_ids = self.semester_obj.search(self.cr, self.uid, [('number', 'in', semester_names), ('academic_year', '=', year_id)])
        for semester in self.semester_obj.browse(self.cr, self.uid, all_semester_ids):
            semesters.append(semester.name)
        return semesters
    
    def _get_stt(self):
        self.stt += 1
        return self.stt
    
    def reset_stt(self):
        self.stt = 0
        return True
    
    def get_logo(self):
        user = self.pool.get('res.users').browse(self.cr, self.uid, self.uid)
        logo = False
        if user.company_id:
            company = self.pool.get('res.company').browse(self.cr, self.uid, user.company_id.id)
            logo = company.logo
        return logo
    
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
